package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketCoupon;
import com.project2.bean.po.MarketCouponExample;
import com.project2.bean.po.MarketCouponUser;
import com.project2.bean.po.MarketCouponUserExample;
import com.project2.bean.vo.AdminPageInfo;
import com.project2.mapper.MarketCouponMapper;
import com.project2.mapper.MarketCouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdminCouponServiceImpl implements AdminCouponService {

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    /**
     * 优惠卷信息回显
     * @param pageBo 分页信息
     * @param name 优惠卷名查询参数，模糊查询
     * @param type 优惠卷类型
     * @param status 优惠卷状态
     * @return
     */
    @Override
    public CommonData getAllCouponByInfo(PageBo pageBo, String name, Short type, Short status) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MarketCouponExample mcExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = mcExample.createCriteria();
        // 显示所有未被删除的优惠卷
        criteria.andDeletedEqualTo(false);
        // 默认排序为addTime降序
        mcExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());

        // 根据参数栏是否有参数添加限定条件
        // 优惠卷名的参数如果不填会是一个空字符串""
        if(name != null && !name.equals("")){
            criteria.andNameLike("%" + name + "%");
        }
        if(type != null){
            // 如果优惠卷类型参数非空，增加限定条件
            criteria.andTypeEqualTo(type);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }

        List<MarketCoupon> coupons = couponMapper.selectByExample(mcExample);

        PageInfo<MarketCoupon> info = new PageInfo<>(coupons);

        return CommonData.data(info);
    }

    /**
     * 获取曾经持有过该优惠卷的所有用户
     * @param couponId 优惠卷id
     * @param userId 用户id
     * @param couponStatus
     * @return
     * @author Frank Zhu
     */
    @Override
    public CommonData<MarketCouponUser> getCouponUsers(Short couponStatus, Integer userId, Integer couponId) {
        MarketCouponUserExample cuExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = cuExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andCouponIdEqualTo(couponId);
        cuExample.setOrderByClause("add_time desc");

        if (couponStatus != null) {
            criteria.andStatusEqualTo(couponStatus);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        List<MarketCouponUser> couponUsers = couponUserMapper.selectByExample(cuExample);

        CommonData<MarketCouponUser> commonData = new CommonData<>();

        commonData.setList(couponUsers);

        return commonData;
    }

    /**
     * 获取指定的优惠卷（单个）
     * @param id
     * @return
     */
    @Override
    public MarketCoupon getSpecifiedCoupon(Integer id) {
        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    /**
     * 根据参数生成不同的优惠卷并且存入数据库
     *
     * 根据优惠卷的type参数，可能需要生成一些对应的参数再进行插入
     * 如果type为2，需要生成对应的优惠码，共八位
     *
     * goods_type值，0为全商品，1为类目限制，2商品限制（但是目前只有全场通用）
     *
     * time_type，有限时间类型，0为领取后多少天有效，这个领取时间就对应coupon_user中的add_Time。
     * 如果为1，则是指定的时间段，start_time、end_time就需要存入值
     * @param coupon
     * @return
     * @author Frank Zhu
     */
    @Override
    public MarketCoupon createCoupon(MarketCoupon coupon) {
        Short type = coupon.getType();
        // 如果是兑换卷类型，增加code参数
        if(type.intValue() == 2){
            String uid = UUID.randomUUID().toString();
            String code = uid.substring(0, 8).toUpperCase();
            coupon.setCode(code);
        }

        // 增加addTime和updateTime
        Date now = new Date();
        coupon.setAddTime(now);
        coupon.setUpdateTime(now);

        couponMapper.insertSelective(coupon);
        return coupon;
    }

    @Override
    public MarketCoupon updateCouponInfo(MarketCoupon updateInfo) {

        MarketCouponExample example = new MarketCouponExample();
        // 别忘了要加上按照id更新这个条件
        example.createCriteria().andIdEqualTo(updateInfo.getId());
        updateInfo.setUpdateTime(new Date());
        couponMapper.updateByExampleSelective(updateInfo,example);
        return updateInfo;
    }

    @Override
    public void deleteCoupon(Integer id) {
        MarketCoupon deleteInfo = new MarketCoupon();
        deleteInfo.setDeleted(true);
        deleteInfo.setId(id);
        deleteInfo.setUpdateTime(new Date());
        couponMapper.updateByPrimaryKeySelective(deleteInfo);
    }


}
